package com.longmao.educms.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.longmao.commonutils.R;
import com.longmao.educms.entity.CrmBanner;
import com.longmao.educms.service.CrmBannerService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author longmao
 * @since 2022-02-11
 */
@RestController
@RequestMapping("/educms/banner")
//@CrossOrigin
public class CrmBannerController {

    @Autowired
    private CrmBannerService bannerService;

    @ApiOperation(value = "获取Banner分页列表")
    @GetMapping("/pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable Long page, @PathVariable Long limit) {

        Page<CrmBanner> pageBanner = new Page<>(page, limit);
        IPage<CrmBanner> page1 = bannerService.page(pageBanner, null);
        return R.ok().data("items", pageBanner.getRecords()).data("total", pageBanner.getTotal());
    }

    @ApiOperation(value = "获取Banner")
    @PostMapping("/getBannerById/{bannerId}")
    public R getBannerById(@PathVariable String bannerId) {
        CrmBanner banner = bannerService.getById(bannerId);
        return R.ok().data("item", banner);
    }

    @ApiOperation(value = "新增Banner")
    @PostMapping("/addBanner")
    public R addBanner(@RequestBody CrmBanner banner) {
        bannerService.save(banner);
        return R.ok();
    }

    @ApiOperation(value = "修改Banner")
    @PostMapping("/updateBanner")
    public R updateBannerById(@RequestBody CrmBanner banner) {
        bannerService.updateById(banner);
        return R.ok();
    }

    @ApiOperation(value = "删除Banner")
    @DeleteMapping("/delBanner/{bannerId}")
    public R delBanner(@PathVariable String bannerId) {
        boolean b = bannerService.removeById(bannerId);
        return R.ok();
    }

    /**
     * 前端查询
     * @return
     */
    @ApiOperation(value = "获取首页banner")
    @GetMapping("/getAllBanner")
    public R getAllBanner() {
        List<CrmBanner> bannerList = bannerService.selectAllBanner();
        return R.ok().data("bannerList", bannerList);
    }
}

